package service;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import dao.MongoDBDao;
import entity.Device;
import org.bson.Document;

import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @program: IoT201759225309
 * @description: 用于提供Device操作
 * @author: Xiaozhuoer
 * @create: 2021-06-01 19:27
 **/

@Service
public class DeviceService implements MongoDBDao{

    private final MongoTemplate mongoTemplate;

    public DeviceService(MongoTemplate mongoTemplate){
        this.mongoTemplate = mongoTemplate;
    }

    /**
     * 连接MongoDB
     * @return MongoDatabase
     */
    @Override
    public MongoDatabase connectMongoDB() {
        MongoClient client = new MongoClient("localhost",27017);
        MongoDatabase database = client.getDatabase("iot");
        return database;
    }

    /**
     * 查询所有集合
     * @return List<Object>
     */
    @Override
    public List<Object>  findAll() {
        List<Object> list = new ArrayList<>();
        MongoDatabase mongoDatabase = connectMongoDB();
        MongoCollection<Document> collection = mongoDatabase.getCollection("device");
        FindIterable<Document> findIterable = collection.find();
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()){
            Document document = mongoCursor.next();
            ObjectId id = (document.getObjectId("_id"));
            document.put("_id",id.toString());
            list.add(document);
        }
        mongoCursor.close();
        return list;
    }

    /**
     * 增加设备信息集合
     * @param device
     */
    @Override
    public void insertDocument(Device device) {
        MongoDatabase mongoDatabase = connectMongoDB();
        mongoTemplate.insert(device);
    }
    /**
     * 更新设备信息集合
     * @param idStr,device
     */
    public void updateDocument(String idStr,Device device){
        MongoDatabase mongoDatabase = connectMongoDB();
        MongoCollection<Document> collection = mongoDatabase.getCollection("device");
        Bson filter = Filters.eq("idString",idStr);
        Document deviceName = new Document("$set", new Document("devicename",device.getDevicename()));
        collection.updateOne(filter,deviceName);
        Document updateTime = new Document("$set", new Document("updatetime",device.getUpdatetime()));
        collection.updateOne(filter,updateTime);
        Document value = new Document("$set", new Document("value",device.getValue()));
        collection.updateOne(filter,value);
        Document status = new Document("$set", new Document("status",device.getStatus()));
        collection.updateOne(filter,status);
        Document type = new Document("$set", new Document("type",device.getType()));
        collection.updateOne(filter,type);
    }
    /**
     * 更新设备信息的idString字段
     * @param device
     */
    public void updateDocumentIdStr(Device device){
        MongoDatabase mongoDatabase = connectMongoDB();
        MongoCollection<Document> collection = mongoDatabase.getCollection("device");
        Bson filter = Filters.eq("idString"," ");
        Document document = new Document("$set", new Document("idString",device.getIdString()));
        collection.updateOne(filter,document);
    }
    /**
     * 新增设备集合
     * @param device
     */
    public void addDevice(Device device){
        Date date = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        device.setUpdatetime(ft.format(date));
        insertDocument(device);
        //添加之后，获取ObjectId的String 给idString字段
        device.setIdString(findIdInLast());
        updateDocumentIdStr(device);
    }
    /**
     * 修改设备信息
     * @param id,device
     * @return Device
     */
    public Device modifyDevice(String id,Device device){
        Date date = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        device.setUpdatetime(ft.format(date));
        updateDocument(id,device);
        return device;
    }
    /**
     * 删除设备信息
     * @param id
     */
    public void deleteDevice(String id){
        MongoDatabase mongoDatabase = connectMongoDB();
        MongoCollection<Document> collection = mongoDatabase.getCollection("device");
        Bson filter = Filters.eq("idString",id);
        collection.deleteOne(filter);
    }
    /**
     * 删除设备的value字段信息
     * @param idStr
     */
    public void deleteValue(String idStr){
        MongoDatabase mongoDatabase = connectMongoDB();
        MongoCollection<Document> collection = mongoDatabase.getCollection("device");
        Bson filter = Filters.eq("idString",idStr);
        Document value = new Document("$set", new Document("value"," "));
        collection.updateOne(filter,value);
    }
    /**
     * 修改设备的value字段信息
     * @param device
     */
    public void addDeviceValue(Device device){
        MongoDatabase mongoDatabase = connectMongoDB();
        MongoCollection<Document> collection = mongoDatabase.getCollection("device");
        Bson filter = Filters.eq("devicename",device.getDevicename());
        Document value = new Document("$set", new Document("value",device.getValue()));
        collection.updateOne(filter,value);
    }
    /**
     * 获取最后一条集合的ID值
     * @return String
     */
    public String findIdInLast() {
        String strId = new String();
        MongoDatabase mongoDatabase = connectMongoDB();
        MongoCollection<Document> collection = mongoDatabase.getCollection("device");
        FindIterable<Document> findIterable = collection.find();
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()){
            Document document;
            document = mongoCursor.next();
            ObjectId id;
            id = document.getObjectId("_id");
            strId = id.toString();
        }
        mongoCursor.close();
        return strId;
    }
    /**
     * 通过设备名字获取设备信息集合
     * @param devicename
     * @return List<Object>
     */
    public List<Object> findOneInDeviceName(String devicename) {
        List<Object> list = new ArrayList<>();
        MongoDatabase mongoDatabase = connectMongoDB();
        MongoCollection<Document> collection = mongoDatabase.getCollection("device");
        Bson filter = Filters.eq("devicename",devicename);
        FindIterable<Document> findIterable = collection.find(filter);
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()){
            Document document = mongoCursor.next();
            list.add(document);
        }
        mongoCursor.close();
        return list;
    }

}
